package com.kstech.gw.utils;

/**
 * Created by lenovo on 2017/1/9.
 */

import android.util.Log;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;


public class MyHttpUtils {
    String results = "";

    /**
     * @param whereRequest 请求名字
     * @param url          请求地址
     * @param map          请求参数map
     * @param callback     回调方法
     * @return
     */
    public void xutilsGet(final String whereRequest, String url, HashMap<String, String> maps, final MyHttpCallback callback) {
        RequestParams params = new RequestParams(url);
        if (null != maps && !maps.isEmpty()){
            for (Map.Entry<String,String> entry : maps.entrySet()){
                params.addQueryStringParameter(entry.getKey(),entry.getValue());
            }
        }
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("", "");
                callback.onSuccess(result, whereRequest);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("", "");
                callback.onError("出错了" + ex.toString(), whereRequest);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("", "");
                callback.onError("出错了" + "onCancelled", whereRequest);
            }

            @Override
            public void onFinished() {
                Log.e("", "");
            }
        });
    }

    /**
     * @param whereRequest 请求名字
     * @param url          请求地址
     * @param map          请求参数map
     * @param callback     回调方法
     */
    public void xutilsPost(final String whereRequest, String url, HashMap<String, String> maps, final MyHttpCallback callback) {
        RequestParams params = new RequestParams(url);
        if (null != maps && !maps.isEmpty()){
            for (Map.Entry<String,String> entry : maps.entrySet()){
                params.addQueryStringParameter(entry.getKey(),entry.getValue());
            }
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("", "");
                callback.onSuccess(result, whereRequest);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("", "");
                callback.onError("出错了" + "onError", whereRequest);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("", "");
                callback.onError("出错了" + "onCancelled", whereRequest);
            }

            @Override
            public void onFinished() {
                Log.e("", "");
            }
        });
    }


    public interface MyHttpCallback {
        void onSuccess(String result, String whereRequest);

        void onError(String errorMsg, String whereRequest);
    }

}
